package cn.ly.blogController;

import cn.ly.entity.User;
import cn.ly.service.UserService;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class BlogController {
    @Autowired
    private UserService userService;


    @RequestMapping("")
    public String login(){
      /*  User user = userService.checkUser("mayouheng","123");
        System.out.println("user ----- >" + user);*/
        return "login";
    }

    @RequestMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password
            , HttpSession session, RedirectAttributes redirectAttributes){
        User user = userService.checkUser(username,password);
        System.out.println("user ----- >" + user);
        if(user != null){
            session.setAttribute("user",user);
            return "index";
        }else {
            redirectAttributes.addFlashAttribute("message","用户名或者密码错误!");
            return "redirect: /admin/login";
        }

    }
    @RequestMapping("/loginOut")
    public String loginOut(HttpSession session){
        session.removeAttribute("user");
        return "login";
    }

    @GetMapping("/blog")
    public String blog(){
        return "blog";
    }
}
